package com.lianxian.xindong.life.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.lianxian.xindong.life.R;
import com.lianxian.xindong.life.R2;
import com.lianxian.xindong.life.adapter.FilterOnePopupWindowAdapter;
import com.lianxian.xindong.life.adapter.FoodRankingContentAdapter;
import com.lianxian.xindong.life.adapter.FoodRankingFilterOneAdapter;
import com.lianxian.xindong.life.adapter.FoodRankingFilterTwoAdapter;
import com.lianxian.xindong.life.entities.FoodRandkingFilterOneRecyclerModel;
import com.lianxian.xindong.life.entities.FoodRankingContentModel;
import com.lianxian.xindong.life.entities.TopTitleModel;
import com.lianxian.xindong.life.myinterface.MyCallBack;
import com.lianxian.xindong.life.view.MyGridView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class FoodRankingActivity extends AppCompatActivity {
    @BindView(R2.id.food_ranking_filter_one)
    RecyclerView recyclerView;
    @BindView(R2.id.show_popupwindow)
    ImageView popImageView;
    @BindView(R2.id.food_ranking_filter_two)
    RecyclerView recyclerViewFilterTwo;
    @BindView(R2.id.rood_ranking_content_recyclerview)
    RecyclerView foodRankingContentRV;

    PopupWindow popupWindow;
    FilterOnePopupWindowAdapter popUpWindowAdapter;
    FoodRankingFilterOneAdapter filterOneAdapter;
    List<FoodRandkingFilterOneRecyclerModel> list;
    List<TopTitleModel> listTopTitleModel;
    FoodRankingFilterTwoAdapter twoAdapter;
    FoodRankingContentAdapter contentAdapter;
    List<FoodRankingContentModel> rankingContentModels;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        setContentView(R.layout.activity_food_ranking);
        ButterKnife.bind(this);
        filterOneAdapter = new FoodRankingFilterOneAdapter(list,this);
        recyclerView.setAdapter(filterOneAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this);
        linearLayoutManager2.setOrientation(RecyclerView.HORIZONTAL);
        recyclerViewFilterTwo.setAdapter(twoAdapter);
        recyclerViewFilterTwo.setLayoutManager(linearLayoutManager2);
        initView();
    }
    private void initView(){
        View popupwindow_view = LayoutInflater.from(this).inflate(R.layout.filter_one_popwindow, null);
        ImageView closeIV = popupwindow_view.findViewById(R.id.popupwindow_close);
        MyGridView gridView = popupwindow_view.findViewById(R.id.filter_one_gridview);
        popUpWindowAdapter = new FilterOnePopupWindowAdapter(list, filterOneAdapter.currentPosition, this, new MyCallBack() {
            @Override
            public void callback(int currentposition) {
                filterOneAdapter.setCurrentPosition(currentposition);
                filterOneAdapter.notifyDataSetChanged();
                recyclerView.scrollToPosition(currentposition);
                popupWindow.dismiss();
            }
        });
        gridView.setAdapter(popUpWindowAdapter);
        popupWindow = new PopupWindow(popupwindow_view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));
        popupWindow.setAnimationStyle(R.style.PopAnimation);
        closeIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        popupWindow.setOutsideTouchable(true);
        popImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popUpWindowAdapter= new FilterOnePopupWindowAdapter(list, filterOneAdapter.currentPosition, FoodRankingActivity.this, new MyCallBack() {
                    @Override
                    public void callback(int currentposition) {
                        filterOneAdapter.setCurrentPosition(currentposition);
                        filterOneAdapter.notifyDataSetChanged();
                        recyclerView.scrollToPosition(currentposition);
                        popupWindow.dismiss();
                    }
                });
                gridView.setAdapter(popUpWindowAdapter);
                popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);
            }
        });
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
        foodRankingContentRV.setAdapter(contentAdapter);
        foodRankingContentRV.setLayoutManager(linearLayoutManager);

    }
    private void initData(){
        List<FoodRandkingFilterOneRecyclerModel> list = new ArrayList<>();
        for (int i=0;i<10;i++){
            FoodRandkingFilterOneRecyclerModel model = new FoodRandkingFilterOneRecyclerModel(i,"日本菜"+i);
            list.add(model);
        }
        this.list = list;
        listTopTitleModel = new ArrayList<>();
        for (int i=0;i<5;i++){
            TopTitleModel model = new TopTitleModel(i,"热门榜"+i);
            listTopTitleModel.add(model);
        }
        twoAdapter = new FoodRankingFilterTwoAdapter(listTopTitleModel,this,1);
        rankingContentModels = new ArrayList<>();
        for (int i=0;i<20;i++){
            FoodRankingContentModel model = new FoodRankingContentModel("天邦老火锅",i,4,23.3f,
                    "较场口","重庆火锅",4.5f,"晚上的夜景很美，很吸引游客眼球","http://pic1.win4000.com/wallpaper/c/5799c80de4900.jpg",i+1);
            rankingContentModels.add(model);
        }
        contentAdapter = new FoodRankingContentAdapter(rankingContentModels,this);
    }
}